package comm

import (
	"time"

	"dev.ixmxm.top/rms/utils/log"
	"dev.ixmxm.top/rms/utils/tools"
	"google.golang.org/grpc"
	"google.golang.org/grpc/backoff"
	"google.golang.org/grpc/keepalive"
)

func GRPCClient(address string) (*grpc.ClientConn, error) {
	conn, err := grpc.Dial(address,
		grpc.WithInsecure(),
		grpc.WithBlock(),
		grpc.WithConnectParams(grpc.ConnectParams{Backoff: backoff.Config{
			BaseDelay:  3.0 * time.Second,
			Multiplier: 1.6,
			Jitter:     0.2,
			MaxDelay:   5 * time.Second,
		},
			MinConnectTimeout: 1 * time.Second}),
		grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(99999999), grpc.MaxCallSendMsgSize(99999999)),
		grpc.WithKeepaliveParams(keepalive.ClientParameters{
			Time:                3 * time.Second,
			Timeout:             5 * time.Second,
			PermitWithoutStream: true}))
	if err != nil {
		log.Errorf("dail %s grpc error:%s", address, err.Error())
		tools.Sleep(3.0)
		return nil, err
	}
	return conn, nil
}
